package com.Fate.dao;

import com.Fate.common.Constant;
import com.Fate.common.PageInfo;
import com.Fate.pojo.Customer;

import java.util.Map;
import java.util.StringJoiner;

/**
 * 分页查询信息
 */
public class CustomerDao extends BaseDao {
    public PageInfo<Customer> selectPage(Map<String, String> params, Integer pageNo, Integer pageSize) {
        String sql = "select c.id, c.name, c.phone, c.sex, c.company, c.deleted, c.user_id as userId, c.create_time as createTime, c.update_time as updateTime, c.delete_time as deleteTime ,u.realname from customer c LEFT JOIN users u ON c.user_id = u.id where 1=1  ";
       // 分隔符 前缀 后缀
        StringJoiner str = new StringJoiner(" and ");
        str.add(sql);//项1 and 项2
        if (params.containsKey("name")) {
            String name = params.get("name");
            str.add(" c.name like '%"+ name +"%' ");
        }
        if (params.containsKey("phone")) {
            String phone = params.get("phone");
            str.add(" c.phone  = " + phone);
        }
        if (params.containsKey("sex")) {
            String sex = params.get("sex");
            str.add(" c.sex = " + sex);
        }
        if (params.containsKey("company")) {
            String company = params.get("company");
            str.add(" c.company like '%"+company+"%' ");
        }
        if (params.containsKey("deleted")) {
            String deleted = params.get("deleted");
            str.add(" c.deleted = " + deleted);
        }
        if (params.containsKey("userId")) {
            String userId = params.get("userId");
            str.add(" c.user_id = " + userId);
        }
        sql = str.toString();
        System.out.println(sql);

        return super.selectPage(sql,Customer.class,pageNo,pageSize);
    }

    /**
     * 插入新客户
     *
     */
    public void insert(String name, String phone, Integer sex, String company,Integer userId) {
        String sql="INSERT INTO `customer` ( `name`, `phone`, `sex`, `company`,`deleted`,`user_id`) VALUES(?,?,?,?,?,?) ;";
        super.update(sql,name,phone,sex,company, Constant.CUSTOMER_DELETE_NOT,userId);
    }

    /**
     * 查询电话对应id
     * @param phone
     * @return
     */
    public Customer selectOne(String phone) {
        String sql = "select id from customer where phone =?";
       return   super.selectOne(sql,Customer.class,phone);
    }

    public Integer update(String name, String phone, String sex, String company,String id) {
        String sql = "UPDATE customer SET `name` = ?, `phone` = ?, `sex` = ?, `company` = ? WHERE `id` = ?";
        return super.update(sql, name,phone,sex,company,id);
    }

    public void Update(String id ,String name, String phone, Integer sex, String company) {
        String sql = "UPDATE `customer` SET `name`=?, `phone`=?, `sex`=?, `company`=? where `id`= ?;";
           super.update(sql,name,phone,sex,company,id);
    }

    public void deleted(String id,Integer deleted) {
        String sql = "UPDATE `customer` SET  deleted=?  where id=?";
        super.update(sql,deleted,id);
    }

    /**
     * 修改客户的业务员
     * @param userId
     * @param cids
     */
    public void updateSalesman(String userId, String... cids) {
        //1.Jdbc 批量操作
        //2.组合一个SQL
        String customerIds = String.join(",",cids);
        String sql = "update customer set user_id = ? where id in("+customerIds+")";
        super.update(sql,userId);


    }




















}
